#include<iostream>
#include<math.h>
#include "Poligon.h"

Poligon::Poligon()
{
  this->nrPuncte = 0;
}

Poligon::Poligon(int)
{
  int n;
  std::cout<<"Numarul punctelor poligonului:";
  std::cin>>n;
  this->nrPuncte = n;
  std::cout<<"\n Introduceti Punctele:";
  for(int i = 0; i < n; i++)
  {
    std::cout<<"\nIntroduceti coordonatele punctului "<<i+1<<":";
    std::cout<<"\nCoordonata x:";
    std::cin>>puncte[i].x;
    std::cout<<"Coordonata y:";
    std::cin>>puncte[i].y;
  }
}

void Poligon::arataPuncte()
{
  std::cout<<"Punctele sunt: \n";
  for(int i = 0; i < this->nrPuncte; i++)
  {
    std::cout<<"Punctul numarul "<<i+1<<"\n";
    std::cout<<puncte[i]<<std::endl;
  }
}

bool Poligon::isTriunghi() const
{
  if (this->nrPuncte == 3)
  {
    Segment d1 = Segment(this->puncte[0], this->puncte[1]);
    Segment d2 = Segment(this->puncte[1], this->puncte[2]);
    Segment d3 = Segment(this->puncte[0], this->puncte[2]);
    if((d1.lungime() + d2.lungime() > d3.lungime()) && (d2.lungime() + d3.lungime() > d1.lungime()) && (d1.lungime() + d3.lungime() > d2.lungime()))
      return true;
  }
  return false;
}

bool Poligon::isDreptunghi() const
{
  if (this->nrPuncte == 4)
  {
    Segment d1 = Segment(this->puncte[0], this->puncte[1]);
    Segment d2 = Segment(this->puncte[1], this->puncte[2]);
    Segment d3 = Segment(this->puncte[2], this->puncte[3]);
    Segment d4 = Segment(this->puncte[0], this->puncte[3]);
    Segment diag1 = Segment(this->puncte[0], this->puncte[2]);
    Segment diag2 = Segment(this->puncte[1], this->puncte[3]);
    
    if((diag1.lungime() == diag2.lungime()) && (d1.lungime() == d3.lungime()) &&(d2.lungime() == d4.lungime()))
      return true;
  }
  return false;
}

bool Poligon::isPatrat() const
{
  if(this->isDreptunghi())
  {
    Segment diag1 = Segment(this->puncte[0], this->puncte[2]);
    Segment diag2 = Segment(this->puncte[1], this->puncte[3]);
    double x1 , x2, y1, y2; 
    x1 = this->puncte[2].x - this->puncte[0].x;
    x2 = this->puncte[3].x - this->puncte[1].x;
    y1 = this->puncte[2].y - this->puncte[0].y;
    y2 = this->puncte[3].y - this->puncte[1].y;
    if( x1 * x2 + y1 * y2 == 0)//daca diagonalele sunt perpendiculare atunci este patrat
      return true;
  }
  return false;
}

bool Poligon::isConvex() const
{
  double sumAngle = 0.0;
  double epsilon = 0.01;
  double pi = 3.14;
  double x1, x2, y1, y2;
  for(int i = 0; i < this->nrPuncte; i++)
  {
    x1 = this->puncte[i + 1].x - this->puncte[i].x;
    x2 = this->puncte[i + 2].x - this->puncte[i + 1].x;
    y1 = this->puncte[i + 1].y - this->puncte[i].y;
    y2 = this->puncte[i + 2].y - this->puncte[i - 1].y;
    sumAngle += acos( (x1 * x2 + y1 * y2) /  sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)));
  }
  if((((this->nrPuncte - 2) * pi - epsilon) < sumAngle) && (sumAngle < (this->nrPuncte - 2) * pi + epsilon))
    return true;
    else return false;
}
